<?php
namespace app\api\controller\xiservice;

use app\common\controller\xiservice\XiserviceApi;
use app\common\model\xiservice\Service as ServiceModel;
use app\common\model\xiservice\ServiceCategory as ServiceCategoryModel;
use app\common\model\xiservice\Collect as CollectModel;
use app\common\model\xiservice\Comment  as CommentModel;
use think\Db;
use think\Cache;
/**
 * 服务模块
 */
class Service extends XiserviceApi
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];


    /**
     * @ApiTitle    (获取首页页面服务分类的服务接口)
     * @ApiMethod   (GET)
     * @ApiRoute    (/api/xiservice.service/index_categorylist)
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", sample="{'id':'int','name':'string','image':'string','desc':'string',child':'object'}", description="返回数据")
     */
    public function index_categorylist()
    {
      $cityName = $this->request->request('cityName');  

            $list = ServiceCategoryModel::where('index_recom',1)
                                    ->where('pid',0)->where('status',1)
                                    // ->where('city',$cityName)
                                    ->order('weigh desc')
                                    ->field(['id','name','image','desc'])
                                    ->select();
            foreach ($list as $row) {
               //获取分类下的服务
                $allCategory = ServiceCategoryModel::where('pid',$row['id'])->select();
                if(!$allCategory){
                   $row['child'] = [];
                }else{
                     $allCategoryIds = array_column($allCategory, 'id');
                    $row['child'] = ServiceModel::where('status',1)
                              ->where('service_category_id','in',$allCategoryIds)  
                              ->limit(6) 
                              ->order('weigh desc')
                              ->field(['id','name','image','bargain','saleprice','basicnums','salenums'])
                              ->select();                
                }

            }

      $this->success('查询成功', $list);
    }   


    /**
     * @ApiTitle    (获取首页页面服务分类的服务接口)
     * @ApiMethod   (GET)
     * @ApiRoute    (/api/xiservice.service/second_categorylist)
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", sample="{'id':'int','name':'string','image':'string','desc':'string',child':'object'}", description="返回数据")
     */
    public function second_categorylist()
    {
        $cityName = $this->request->request('cityName'); 
        


        $serviceCategoryId = $this->request->request('id',0); 
        $allCategory = ServiceCategoryModel::where('pid',$serviceCategoryId)->where('status',1)
        //->where('city',$cityName)
        ->order('weigh desc')->field(['id','name'])->select();
         if(!$allCategory ){
            $this->success('查询成功', []);
         }

        for($i=0;$i<count($allCategory);$i++){
             $allCategory[$i]['service'] = ServiceModel::where('status',1)
                                          ->where('service_category_id',$allCategory[$i]['id'])
                                          ->order('weigh desc')
                                          ->where('city_name',$cityName)
                                          ->field(['id','name','image','bargain','saleprice','basicnums','salenums'])
                                          ->select();
        }

        $this->success('查询成功',$allCategory);
    }      

    /**
     * @ApiTitle    (获取服务列表)
     * @ApiMethod   (GET)
     * @ApiRoute    (/api/xiservice.service/list)
     * @ApiParams   (name="service_category_id", type="int", description="服务分类id")
     * @ApiParams   (name="type", type="int", description="级别")
     * @ApiParams   (name="key", type="string", description="搜索关键字")
     * @ApiParams   (name="page", type="int", description="页码")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", sample="{'id':'int','name':'string','image':'string','bargain':'int','saleprice':'float','basicnums':'int','salenums':'int'}", description="返回数据")
     */
	public function list()
	{
      $serviceCategoryId = $this->request->request('service_category_id',0);  
      
      $type = $this->request->request('type',2);  
      $key = $this->request->request('key');  
      $cityName = $this->request->request('cityName','');  
      $alllist = [];
      $oneCategory = Db::query('
      select * from fa_xiservice_service_category where id=:id',['id'=>$serviceCategoryId]);
      $alllist['Category'] =  $oneCategory[0];
      $list= Db::query('
      select f1.*,f.name as categoryname,f.image categoryimage from fa_xiservice_service_category f inner join fa_xiservice_service f1
          on f.id=f1.service_category_id where city_name=:cityName and pid=:pid',['pid'=>$serviceCategoryId,'cityName'=>  $cityName]);
          $alllist['list']=$list;
	  $this->success('查询成功', $alllist);
	}	

  public function expertlist()
	{
      $serviceCategoryId = $this->request->request('service_category_id',0);  
      $type = $this->request->request('type',2);  
      $key = $this->request->request('key');  
      $cityName = $this->request->request('cityName','');  

      $alllist = [];
      $oneCategory = Db::query('
      select * from fa_xiservice_service_category where id=:id',['id'=>$serviceCategoryId]);
      $user_id = $this->auth->id;

      $alllist['Category'] =  $oneCategory[0];

      $list= Db::query('
      select f.* from fa_xiservice_user f inner join fa_xiservice_service_category f1
         on FIND_IN_SET(f1.id, f.service_category_id) >0
         where user_id=:userid and f1.id=:service_category_id;',['service_category_id'=>$serviceCategoryId,
         'userid'=>  $user_id]);
          $alllist['list']=$list;
	  $this->success('查询成功', $alllist);
	}	


    /**
     * @ApiTitle    (获取服务详情)
     * @ApiMethod   (GET)
     * @ApiRoute    (/api/xiservice.service/detail)
     * @ApiParams   (name="id", type="int", description="服务id")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", sample="{'id':'int','name':'string','image':'string','images':'string','bargain':'int','saleprice':'float','basicnums':'int','salenums':'int','content':'string'}", description="返回数据")
     */
    public function detail()
    {
      $id = $this->request->request('id');  
      $list =ServiceModel::where('id',$id)
                        ->where('status',1)
                        ->field(['id','name','image','images','bargain','saleprice','basicnums','salenums','content'])
                        ->find();
      if($list){
         $list['comment'] =CommentModel::with(['user'=>function($query){
                                              $query->withField('user_id,username,avatar');
                                        }])
                                      ->where('service_id',$id)
                                      ->limit(3)
                                      ->select();
        $list['collect'] = 0;
         if($this->auth->id){
             $res = CollectModel::where('user_id',$this->auth->id)
                                  ->where('service_id',$id)
                                  ->find();
              if($res){
                  $list['collect'] = 1;
              }
         }
      }
      $this->success('查询成功', $list);
    }   

    /**
     * @ApiTitle    (获取服务评价列表)
     * @ApiMethod   (GET)
     * @ApiRoute    (/api/xiservice.service/comment)
     * @ApiParams   (name="id", type="int", description="轮播id")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", sample="{}", description="返回数据")
     */
    public function comment()
    {
      $id = $this->request->request('id');  
      $list=CommentModel::with(['user'=>function($query){
                                              $query->withField('user_id,username,avatar');
                                        }])
                                      ->where('service_id',$id)
                                      ->paginate();;
      
      $this->success('查询成功', $list);
    }   

    /**
     * @ApiTitle    (获取服务评价)
     * @ApiMethod   (GET)
     * @ApiRoute    (/api/xiservice.service/comment)
     * @ApiParams   (name="id", type="int", description="轮播id")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", sample="{}", description="返回数据")
     */
    public function worker_comment()
    {
      $id = $this->request->request('id');  
      $list=CommentModel::with(['user'=>function($query){
                                              $query->withField('user_id,username,avatar');
                                        }])
                                      ->where('order_id',$id)
                                      ->paginate();
      
      $this->success('查询成功', $list);
    }   

    public function getshoplist()
    {
      $list= Db::name('goods_shop')->where(['is_top'=>1])->select();
      
      $this->success('查询成功', $list);
    }   
    public function getshopdetail()
    {
      $id = $this->request->request('id');  
      $shop =  Db::name('goods_shop')->where(['id'=>$id])->find();      
      $this->success('查询成功', $shop);
    }   
    public function getproductlist()
    {
      $id = $this->request->request('id');  
      $shop =  Db::name('goods_shop')->where(['id'=>$id])->find();

      if($id)
      {
        $list= Db::name('goods')->where(['shop_name_id'=>$id])->select();


      }else
      {
        $list= Db::name('goods')->select();


      }
      $alllist['shop']= $shop;
      $alllist['productlist']= $list;

      $this->success('查询成功', $alllist);
    }  
    public function getproductdetail()
    {
      $id = $this->request->request('id');  

      $good= Db::name('goods')->where(['id'=>$id])->find();
      
      $this->success('查询成功', $good);
    }   

    public function getpxlist()
    {
        $list = Db::name('train')->select();
        $this->success('查询成功', $list);

    }
    
    public function getpxdetail()
    {
      $id = $this->request->request('id');  

        $info = Db::name('train')->where(['id'=>$id])->find();
        $this->success('查询成功', $info);

    }
    public function getpxtime()
    {
      $id = $this->request->request('id');  
      $list = Db::name('train_time')->where(['train_id'=>$id])->select();
      foreach($list as $item)
      {
        $content[]=$item['timetrain'];
      }
      $this->success('查询成功', $content);


    }
}